import threading


def singleton(cls):
    _instance = {}
    _lock = threading.Lock()
    def inner():
        with _lock:
            if cls not in _instance:
                _instance[cls] = cls()
            return _instance[cls]

    return inner

def locked(func):
    def wrapper(self, *args, **kwargs):
        with self.lock:
            return func(self, *args, **kwargs)

    return wrapper